<?php

require_once APPPATH . 'libraries/PartnerParser/Abstract.php';

/**
 * Dircash парсер
 *
 * @author Gennadiy Kozlenko
 */
class PartnerParser_Dircash extends PartnerParser_Abstract {
    
    /**
     * Youtube code
     * @var string
     */
    const YOUTOBE_CODE = '<object width="640" height="480"><param name="movie" value="http://www.youtube.com/v/[CODE]?fmt=18"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/[CODE]?fmt=18" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="480"></embed></object>';

    /**
     * Parse partners link
     */
    public function parse() {
        $content = $this->getContent($this->xmlLink);
        if (!empty($content)) {
            $xml = @simplexml_load_string($content);
            if ($xml) {
                $movies = $xml->xpath('/movie_list/movie');
                if ($movies) {
                    foreach ($movies as $movie) {
                        $externalId = (string) $movie->id;
                        $entry = new VideoEntry();
                        $title = trim(stripslashes((string) $movie->title));
                        $entry->setTitle($title);
                        $origTitle = trim(stripslashes((string) $movie->title_orig));
                        if ($origTitle != $title) {
                            $entry->setOrigTitle($origTitle);
                        }
                        $entry->setYear((int) $movie->year);
                        $code = (string) stripslashes(trim($movie->film_tizer));
                        $videos = $this->searchVideos($entry);
                        foreach ($videos as $idVideo) {
                            if (!empty($code)) {
                                $trailer = str_replace('[CODE]', $code, self::YOUTOBE_CODE);
                                $this->obj->db->where('id_video', $idVideo)
                                    ->update('videos', array('trailer' => $trailer));
                            }
                            $this->registerPartner($idVideo, $this->idPartner, $externalId);
                        }
                    }
                } else {
                    $this->obj->log('Error: Empty movies list');
                }
            } else {
                $this->obj->log('Error: Cannot parse XML');
            }
        } else {
            $this->obj->log('Error: Empty content');
        }
    }
    
}